<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>绝地坦克</title>
		<link rel="shortcut icon" href="img/favicon.ico">
		<style>
			body,html{
  				height: 95%;
			}
			body{
 				 background: url('img/21.jpg') no-repeat fixed;
 				 background-size：cover;
				}	
		</style>
	</head>
	<body>
		<script type="text/javascript">
			
			//定义画布      (**余地，很重要！**)
			var can = document.createElement("canvas");
			can.width = screen.width*0.85;//指定画布的宽度
			can.height = screen.height*0.8;//指定画布的高度
			//can.width = screen.width;
			//can.height = screen.height;
			can.style.backgroundColor = "white";//背景chartreuse
			can.style.position = "absolute";
			can.style.left = (screen.width-can.width) / 2 + "px";
			can.style.top = (screen.height-can.height-100) / 2 + "px";
			document.body.appendChild(can);//将画布添加到文档中显示出来
			
			//取得画布对象的画笔  ctrl+c 复制一行        ctrl+v 粘贴一行    shift+alt+上下  代码同词上下        ctrl+o 查找变量
			var g = can.getContext("2d");
			var UNIT 	= 0;		// 每个物体的编号
		var WOJUN 	= "我军";
		var QIANG 	= "墙块";
		var CMD		= "指挥官";
		var DIJUN 	= "敌军";
		var ZIDAN	= "子弹";
		var DILEI	= "地雷";
		var DAOJU	= "道具";
		var TUPIAN	= "图片";
		var FEIJI	= "飞机";
		var DAODAN	= "炸弹";
		
		var TANK_W = 40;
		var TANK_H = 40;
		var DI_W = 40;
		var DI_H = 40;
		var WO_IMG = "img/wU.gif";
		var DI_IMG = "img/tankD.gif";
		
		var FJ_W = 80;
		var FJ_H = 80;
		var MM_W = 50;
		var MM_H = 70;
		var TP_W = 50;
		var TP_H = 50;
		var DJ_W = 24;
		var DJ_H = 24;
		
		var WALL_W = 30;
		var WALL_H = 30;
		var zhb = [];				// 保存8块指挥部的墙
		var kill  = [12, 17, 22, 27, 32];
		var def   = [0,   2,  5,  8, 10];
		var z1	  = [4, 6, 8, 10, 12];
		var z2 	  = [10,12,15,19, 22];
		var zt = ["未开始", "激战", "暂停", "胜利", "失败", "冰冻", "被冰冻", "无敌", "待复活"];
		var wallcount = 30;			// 墙块数量
		var enemycount = 16;		// 敌军数量
		var jianmie	= 0;			// 歼灭数量
		var zhuangtai = zt[0];		// 游戏默认状态 
		var kaishitimer = null;		// 开始文字闪烁的定时器
		var kaishicolor = false;
		var tempstate	= zt[1];	// 状态临时记录变量
		var bdtimer 	= null;		// 冰冻定时器
		var bdshijian	= 5;		// 冰冻时间(秒)
		var djshijian	= 9;		// 倒计时时间(秒)
		var djstimer	= null;		// 倒计时定时器
		var overy 		= 750;		// 游戏结束时的文字y轴
		var overtimer 	= null;		// 游戏结束时的文字定时器
		var shantimer 	= null;		// 无敌闪烁定时器
		var shan =		false;		// 是否闪烁
		var shanshijian = 3;		// 闪的时间
		var timer9		= null;		// 控制闪烁时间的定时器
		
		var gametime	= 180;		// 最开始的时间(秒)
		var gametimer 	= null;		// 游戏定时器
		
		var jifen 		= 0;		// 积分(杀死普通坦克得100分，英雄坦克得150分)
		var fjcount 	= 0;		// 飞机数量
		
		function Stage(){
			this.data = [];
		}
		
		Stage.prototype.add = function(o){
			this.data.push(o);	// 把对象o扔进数组
		}
		
		Stage.prototype.remove = function(o){
			var index = this.data.indexOf(o);
			if ( index != -1 ){
				this.data.splice(index, 1);		// 从数组中删除一个元素
			}
		}
		
		Stage.prototype.render = function(){	// 屏幕重复渲染
			var list = this.data;
			function loop(){
				// 清屏
//				g.fillStyle = BEIJING;
//				g.fillRect(0, 0, can.width , can.height );
				
				var bg = new Image();
				bg.src = "img/ditudark.jpg";
				g.drawImage(bg, 0, 0, can.width, can.height);
				
				Background_image();	drawInfo();	 explain();		author();
				drawStart();	reckon_time();	 suspend();		countdown();
				Game_over();		huatime();		integral();		winer();
				
				list.forEach(function(o){
					o.show();
				});
				
				requestAnimationFrame(loop);
			}
			
			loop();
		}
		
		function Background_image() {
//			var x = (can.width-90*4) / 2 , y = can.height/2 + 50;
//			g.fillStyle = "#333";
//			g.font = "90px 楷体";
//			g.fillText( "坦克大战", x, y );
		}
		
		function drawInfo() {
			var x = 20;    y = 20;
			g.font = "12px 宋体";
			g.fillStyle = "aliceblue";
			g.fillText("敌军数量:" + enemycount , x, y);	x += 80;
			g.fillText("歼灭数量:" + jianmie , x, y);		x += 80;
			g.fillText("生命值:"   + wo.life , x, y);		x += 80;
			g.fillText("难度:82%", x, y);		x += 80;
			g.fillText("复活次数:" + wo.fuhuo, x, y);	x += 80;
			g.fillText("攻击等级:" + wo.gong , x, y);		x += 80;
			g.fillText("防御等级:" + wo.fang , x, y);		x += 80;
			g.fillText("移动速度:" + wo.speed , x, y);	x += 80;
			g.fillText("可用地雷:" +  wo.mine , x, y);		x += 80;
			g.fillText( "游戏状态: " , x, y );	x += 80;
			g.fillStyle = "coral";
			g.fillText( zhuangtai, x, y );	x += 80;
			
		}
		
		function explain() {
			var x = 20, y = 400;
			g.fillStyle = "#999";
			g.font = "18px 楷体";
			g.fillText( "操作说明", x + 15, y );			y += 25;
			g.font = "12px 宋体";
			g.fillText("  发射子弹: 空格" , x, y);	y += 20;
			g.fillText("  暂停游戏: P" , x, y);	y += 20;
			g.fillText("  复活坦克: R" , x, y);	y += 20;
			g.fillText("  埋放地雷: B", x, y);	y+= 20;
			g.fillText("  放大绝技: E(按一次失血3)" , x, y);	y += 20;//69
			g.fillText("  坦克操作:", x, y);	y+= 20;
			g.fillText("           A向左", x, y);	y+= 20;
			g.fillText("           W向上", x, y);	y+= 20;
			g.fillText("           D向右", x, y);	y+= 20;
			g.fillText("           S向下", x, y);	y+= 20;
		
			
		}
		
		function author() {
			var x = 10;    y = 810;
			g.fillText("作者: Raodi", x, y);	y+= 20;
			g.fillText("博客: https://www.cnblogs.com/Raodi", x, y);	y+= 20;
		}
		
		function drawStart() {
			if ( zhuangtai != zt[0] ) return;
			var x = (can.width-40*8.5) / 2 , y = can.height/2 - 80;
			if ( kaishicolor ) {
				g.fillStyle = "#FFFF00";
				g.font = "40px 黑体";
				g.fillText( "按【Y】键开始游戏", x, y );
			}
			
			var bg = new Image();
			bg.src = "img/bg1.gif";
			g.drawImage(bg, (can.width-900)/2, can.height-400-50, 900,400);
		}
		
		function reckon_time() {
			if ( zhuangtai != zt[5] && zhuangtai != zt[6] ) return;
			var x = (can.width-50) / 2 , y = can.height - 180;
			g.fillStyle = zhuangtai == zt[6] ? "darkorange" : "aquamarine";
			g.font = "40px 黑体";
			g.fillText( bdshijian, x, y );
		}
		
		function suspend() {
			if ( zhuangtai != zt[2] ) return;
			var x = (can.width-40*8.5) / 2 , y = can.height/2 - 80;
			g.fillStyle = "#5CB85C";
			g.font = "40px 黑体";
			g.fillText( "按【P】键继续游戏", x, y );
		}
		
		function countdown() {
			if ( zhuangtai != zt[8] ) return;
			var x = (can.width-150) / 2 , y = can.height - 280;
			g.fillStyle = zhuangtai == zt[6] ? "darkorange" : "aquamarine";
			g.font = "60px 黑体";
			g.fillText( djshijian, x, y );
		}
		
		function Game_over() {
			if ( zhuangtai != zt[4] ) return;
			var x = (can.width-400) / 2;
			g.fillStyle = "red";
			g.font = "40px Consolas";
			g.fillText( "你已淘汰，即将离开军事体验区！", x, overy );
			setTimeout('window.location="demo.html"',5000)
		}
		
		function huatime() {
			var x = can.width-60, y = 30;
			g.fillStyle = "lightgreen";
			g.font = "18px 微软雅黑";
			g.fillText( gametime, x, y );
		}
		
		function integral() {
			var x = 900;    y = 20;
			g.fillStyle = "greenyellow";
			g.font = "16px 微软雅黑";
			g.fillText( "积分: " + jifen, x, y );
		}
		
		function winer() {
			if ( zhuangtai != zt[3] ) return;
			var x = (can.width-80*9) / 2 , y = can.height/2 - 50;
			g.fillStyle = "#449D44";
			g.font = "40px 楷体";
			g.fillText( "第一名，恭喜你！ 英勇的绝地坦克特种兵！", x, y );
		}
		
		/**父类**/
		function Base(x, y, w, h, src, type) {
			this.x = x;
			this.y = y;
			this.w = w;
			this.h = h;
			this.src = src;				// 图片名
			this.type = type;			// 类别
			this.life = 100;			// 生命值
			this.index = UNIT ++;		// 唯一的编号
			this.img = new Image();
			this.img.src = this.src;
			this.show = function(){
				g.drawImage(this.img, this.x, this.y, this.w, this.h);
				if ( this.type == WOJUN || this.type == Enemy || this.type == QIANG 
										|| this.type == CMD || this.type == Plane ){
					this.blood();
				}
			}
			this.die = function(){
				stage.remove(this);
				clearInterval(this.movetimer);
				if ( this.type == Enemy || this.type == WOJUN || this.type == Plane ) {
					if ( this.life <= 0 ) {
						this.boom();
						bofang("bomb2.wav");
						if ( this.type == Plane ) {
							jifen += 180;
						}
					}
				} else if ( this.type == DAODAN ){
					this.w = 60; this.h = 60;	this.x -= this.w /2; this.y -= this.h /2;
					this.boom();
					bofang("bomb1.wav");
				}
				this.life = 0;
				if ( this.type == Enemy ){
					enemycount --;
					jianmie ++;
					if ( enemycount == 0 && zhuangtai != zt[4] ){
						zhuangtai = zt[3];
						clearInterval(gametimer);
						bofang("winend.wav");
						bofang("bg.mp3");
					}
					clearInterval(this.kongzhitimer);
					jifen += this.hero ? 150 : 100;
				} else if ( this.type == WOJUN ){
					zhuangtai = wo.fuhuo > 0 ? zt[8] : zt[4];
					bofang("die.wav");
					if ( wo.fuhuo > 0 ){
						djstimer = setInterval(function(){
							djshijian --;	bofang("peng.wav");
							if ( djshijian == 0 ){
								clearInterval(djstimer);
								wo.fuhuo = 0;
								zhuangtai = zt[4];
								over();
							}
						}, 1100);
					} else {
						over();
					}
				} else if ( this.type == CMD ){
					wo.fuhuo = 0;
					wo.life = 0;
					wo.die();
					zhuangtai = zt[4];
					bofang("hithe.wav");
					bofang("hedan.wav");
					over();
				} else if ( this.type == QIANG ){
					bofang("zhahui.wav");
					this.boom();
				}
			}
		}
		
		function mine(tank) {
			var x = tank.x + (tank.w-DJ_W) / 2,	y = tank.y + (tank.h-DJ_H) / 2;
			var o = new Base(x-2, y-2, DJ_W + 2, DJ_H + 2, "img/x10.gif", mine);
			o.n = 10;
			return o;
		}
		
		function Daoju(tank, ishome) {
			var x = tank.x + (tank.w-DJ_W) / 2,	y = tank.y + (tank.h-DJ_H) / 2;
			var n = parseInt( Math.random() * 10 );
			while ( n == 5 && ishome ) {
				n = parseInt( Math.random() * 10 );
			}
			var o = new Base(x - 1, y - 1, DJ_W, DJ_H, "img/x"+ n + ".gif", DAOJU);
			o.n = n;
			return o;
		}
		
		function Blast(tank){
			var x = tank.x - 5,	y = tank.y - 5;
			var o = new Base(x , y , tank.w + 10, tank.h+10, "img/b0.gif", TUPIAN);
			var i = 0;
			o.movetimer = setInterval( function(){
				if ( zhuangtai == zt[2] ) return;	// 爆炸效果暂停
				i ++;
				o.img.src = "img/b" + i + ".gif";
				if ( i == 8 ) {
					o.die();
					if ( tank.type != mine && tank.type != DAODAN ){
						var dj = new Daoju(o, tank.ishome);
						stage.add(dj);
					}
				}
			} , 120);
			return o;
		}
		
		function Bullet(tank) {
			var x = tank.x, y = tank.y;
			if ( tank.dir == "U" ){
				x += tank.w / 2 - 2; 	y -= 17;
			} else if ( tank.dir == "R" ){
				x += tank.w + 2; 	y += tank.h / 2 - 2;
			} else if ( tank.dir == "L" ){
				x -= 14; 			y += tank.h / 2 - 3;
			} else if ( tank.dir == "D" ){
				x += tank.w / 2 - 3; 	y += tank.h - 1;
			}
			
			if ( tank.dir == "D" ) {
				if ( tank.gong == 0 ){
					x += 1;
				} else if ( tank.gong == 3 ){
					x -= 2;
				} else if ( tank.gong == 4 ){
					x -= 3;
				}
			} else if ( tank.dir == "U" ) {
				if ( tank.gong == 1 ){
					x -= 1;
				} else if ( tank.gong == 2 ){
					x -= 2;
				} else if ( tank.gong == 3 ){
					x -= 3;  y -= 3;
				} else if ( tank.gong == 4 ){
					x -= 4;  y -= 8;
				}
			} else if ( tank.dir == "L" ) {
				if ( tank.gong == 3 ){
					x -= 5;  y -= 1;
				} else if ( tank.gong == 4 ){
					y -= 2;	x -= 10;
				}
			} else if ( tank.dir == "R" ) {
				if ( tank.gong == 2 ){
					y -= 1;
				} else if ( tank.gong == 3 ){
					y -= 2;
				} else if ( tank.gong == 4 ){
					y -= 4;
				}
			}
			
			var w = 0, h = 0;
			if ( tank.dir == "D" || tank.dir == "U" ) {
				w = z1[tank.gong];	h = z2[tank.gong];
			} else {
				w = z2[tank.gong];	h = z1[tank.gong];
			}
			
			var src = "img/m" + tank.gong + tank.dir + ".png";
			var o = new Base(x, y, w, h, src, Bullet);
			o.tank = tank;				// 记录子弹的发射者
			o.gong = kill[tank.gong];	// 子弹的伤害点数==坦克的等级对应的数字
			o.speed = 15;
			o.left  = tank.dir == "L";
			o.right = tank.dir == "R";
			o.up    = tank.dir == "U";
			o.down  = tank.dir == "D";
			o.movetimer = setInterval(move, 30);
			function move(){
				if ( zhuangtai == zt[2] ) return;
				var ox = o.x, oy = o.y;	// 保存移动前的旧坐标
				if ( o.left ){
					o.x -= o.speed;
				} else if ( o.right ){
					o.x += o.speed;
				} else if ( o.up ){
					o.y -= o.speed;
				} else if ( o.down ){
					o.y += o.speed;
				}
				if ( checkOut(o) ){		// 移动后，发现坐标超出范围，就死掉
					o.die();
				}
				var r = pengzhuang(o);
				if ( r.peng ){			// 是否发其他物体发生了碰撞，如果有就死掉
					o.die();
					r.o.shixue(o.gong);	// 调用被击中的对象的失血方法
					if ( r.o.type == WOJUN ){
						bofang("hits.wav");
					} else if ( r.o.type == CMD ){
						bofang("hithe.wav");
						bofang("hitcmd.wav");
					} else if ( r.o.type == QIANG ){
						bofang("di.wav");
					}
				}
			}
			return o;
		}
		
		function Blood_strip(x, y, w, h, src, type) {
			var o = new Base(x, y, w, h, src, type);
			o.blood = function(){		// 绘制本对象的血条
				if ( o.life <= 0 ) return;
				g.fillStyle = "red";
				if ( o.life > 80 ) g.fillStyle = "springgreen";
				else if ( o.life > 60 ) g.fillStyle = "yellow";
				else if ( o.life > 40 ) g.fillStyle = "orangered";
				g.fillRect(o.x, o.y-5, o.life / 100 * o.w , 5);
			}
			o.shixue = function(many) {
				if ( zhuangtai == zt[7] && o.type == WOJUN ) return;
				o.life -= many;
				o.life += def[o.fang];
				if ( o.life <= 0 ) {
					o.die();
				}
			}
			return o;
		}
		
		function Daodan(fj){
			var x = fj.x + fj.w / 3 + 5,  y = fj.y + fj.h / 3;
			var w = fj.dir == "U" || fj.dir == "D" ? 25 : 50;
			var h = fj.dir == "U" || fj.dir == "D" ? 50 : 25;
			var src = "img/daodan" + fj.dir + ".png";
			
			y = fj.dir == "U" ? y + 30 : y;
			y = fj.dir == "D" ? y - 50 : y;
			x = fj.dir == "L" ? x + 40 : x;
			x = fj.dir == "R" ? x - 50 : x;
			
			var o = new Base(x, y, w, h, src, DAODAN);
			
			o.dir = fj.dir;
			o.speed = 2;
			o.left = fj.left;
			o.right = fj.right;
			o.up = fj.up;
			o.down = fj.down;
			o.gong = 30 + parseInt(Math.random()*30);
			o.ww = fj.dir == "U" || fj.dir == "D" ? 1 : 2;
			o.hh = fj.dir == "U" || fj.dir == "D" ? 2 : 1;		// 炸弹落地过程中变小的尺寸
			o.endw = fj.dir == "U" || fj.dir == "D" ? 5 : 10;	// 炸弹落地时停止变小的尺寸
			o.xx = fj.dir == "U" || fj.dir == "D" ? 1 : 0;
			o.yy = fj.dir == "U" || fj.dir == "D" ? 0 : 1;
			o.movetimer = setInterval(move, 50);
			function move(){
				if ( zhuangtai == zt[2] ) return ;
				if ( o.left ){
					o.x -= o.speed;
				} else if ( o.right ){
					o.x += o.speed;
				} else if ( o.up ){
					o.y -= o.speed;
				} else if ( o.down ){
					o.y += o.speed;
				}
				if ( checkOut(o) ){		// 移动后，发现坐标超出范围，就恢复为旧坐标
					o.die();
				}
				o.w -= o.ww; o.h -= o.hh;	// 落地前变小尺寸
				if ( o.dir == "L" || o.dir == "R" )
					o.x += o.xx; o.y += o.yy;
				if (o.w <= o.endw ) {
					o.die();
				}
			}
			
			o.boom = function(){
				var zd = new Blast(o);
				stage.add(zd);
				var r = zhadanpengzhuang(o);
			}
			
			return o;
		}
		
		function Plane () {
			var n = parseInt(Math.random() * 4 );	// 决定飞机的图
			var f = parseInt(Math.random() * 4 ); 	// 决定飞机的朝向
			var x = 200 + parseInt(Math.random() * (can.width - 400));
			var y = 100 + parseInt(Math.random() * (can.height - 200));
			var fx = "U";
			if ( f == 0 ) {							// 上
				y = can.height - FJ_H;
			} else if ( f == 1 ){					// 下
				y = 0;	fx = "D";
			} else if ( f == 2 ){					// 左
				x = can.width - FJ_W;	fx = "L";
			} else if ( f == 3 ){					// 右
				x = 0;	fx = "R";
			}
			var o = new Blood_strip(x, y, FJ_W, FJ_H, "img/f" + n + fx + ".png", Plane);
			o.dir = fx;
			o.speed = 3;
			o.left  = fx == "L";
			o.right = fx == "R";
			o.up    = fx == "U";
			o.down  = fx == "D";
			o.fang = 0;
			o.c = 0;		// 投弹的频率控制
			o.movetimer = setInterval(move, 17);
			
			function move(){
				if ( zhuangtai == zt[2] || zhuangtai == zt[5] ) return ;
				if ( o.left ){
					o.x -= o.speed;
				} else if ( o.right ){
					o.x += o.speed;
				} else if ( o.up ){
					o.y -= o.speed;
				} else if ( o.down ){
					o.y += o.speed;
				}
				if ( o.x < -(o.w-10) || o.y < -(o.h-10) || o.x > can.width-10 || o.y > can.height-10){
					o.die();
					fjcount --;
				}
				
				o.c ++;
				if ( o.c % 40 == 0 && parseInt(Math.random()*100) % 2 == 0 ) {
					var dd = new Daodan(o);
					stage.add(dd);
					bofang("hongzha.wav");
				}
			}
			
			o.boom = function(){
				var zd = new Blast(o);
				stage.add(zd);
				fjcount --;
			}
			
			return o;
		}
		
		
		function Command(x, y, fang ){
			var o = new Blood_strip(x, y, MM_W, MM_H, "img/p1.gif", CMD);
			o.fang = fang;
			return o;
		}
		
		function Wall(x, y, fang) {
			var o = new Blood_strip(x, y, WALL_W, WALL_H, "img/qiang.gif", QIANG);
			o.fang = fang;
			o.boom = function(){
				var zd = new Blast(o);
				stage.add(zd);
			}
			return o;
		}
		
		function Tank(x, y, w, h, src, type, speed, gong, fang, dir, filename){
			var o = new Blood_strip(x, y, w, h, src, type);
			o.speed = speed;
			o.gong = gong;
			o.fang = fang;
			o.dir = dir;		// 坦克的方向 UDLR 上下左右
			o.filename = filename;
			o.left = false;
			o.right = false;
			o.up = false;
			o.down = false;
			o.movetimer = setInterval(move, 40, o);
			
			o.toLeft = function(){
				o.dir = "L";	o.img.src = "img/" + o.filename + o.dir + ".gif";
				o.left = true;	o.right = false;	o.up = false;	o.down = false;
			}
			o.toRight = function(){
				o.dir = "R";	o.img.src = "img/" + o.filename + o.dir + ".gif";
				o.left = false;	o.right = true;	o.up = false;	o.down = false;
			}
			o.toUp = function(){
				o.dir = "U";	o.img.src = "img/" + o.filename + o.dir + ".gif";
				o.left = false;	o.right = false;	o.up = true;	o.down = false;
			}
			o.toDown = function(){
				o.dir = "D";	o.img.src = "img/" + o.filename + o.dir + ".gif";
				o.left = false;	o.right = false;	o.up = false;	o.down = true;
			}
			o.stopLeft = function(){
				o.left = false;
			}
			o.stopRight = function(){
				o.right = false;
			}
			o.stopUp = function(){
				o.up = false;
			}
			o.stopDown = function(){
				o.down = false;
			}
			o.fire = function(){
				if ( o.life <= 0 ) return;
				var zd = new Bullet(o);
				stage.add(zd);
			}
			o.boom = function(){
				var zd = new Blast(o);
				stage.add(zd);
			}
			o.eat = function(dj){
				jiandaoju(o, dj);
			}
			return o;
		}
		function Allies(x, y) {
			var o = new Tank(x, y, TANK_W, TANK_H, WO_IMG, WOJUN, 3, 0, 0, "U", "w");
			o.mine = 2;
			o.fuhuo = 3;
			o.relife = function(){
				o.life = 100;
				o.gong = 1;
				o.fang = 0;
				o.speed = 4;
				o.mine = 2;
				o.fuhuo --;
				o.left = false; o.right = false; o.up = false; o.down = false;
				// 检验坐标是否碰撞
				check_up(o);
				stage.add(o);
				clearInterval(djstimer);
				djshijian = 9;
				o.movetimer = setInterval(move, 40, o);
				zhuangtai = zt[7];
				shantimer = setInterval(function(){
					shan = !shan;
					if ( shan ){
						stage.remove(o);
					} else {
						stage.add(o);
					}
				} , 20);
				timer9 = setInterval(function(){
					shanshijian --;
					if ( shanshijian == 0 ){
						check_up(o);
						stage.remove(o);	stage.add(o);
						shanshijian = 3;	shan = false;				// 为下一次闪做准备
						clearInterval(shantimer);
						clearInterval(timer9);
						zhuangtai = enemycount > 0 ? zt[1] : zhuangtai;	// 恢复激战状态
					}
				}, 900);
			}
			
			o.mailei = function(){
				if ( o.mine < 1 ) return;
				var lei = new mine(o);
				stage.add(lei);
				o.mine --;
				bofang("bready.wav");
			}
			
			o.dajueji = function(){
				if ( o.life <= 3 ) return;
				o.life -= 3;	var fx = o.dir;
				o.dir = "U"; o.fire(o);
				o.dir = "L"; o.fire(o);
				o.dir = "R"; o.fire(o);
				o.dir = "D"; o.fire(o);
				o.dir = fx;
			}
			
			return o;
		}
		function Enemy(x, y) {
			var o = new Tank(x, y, DI_W, DI_H, DI_IMG, Enemy, 3, 0, 0, "D", "tank");
			var n = parseInt(Math.random()*100);
			o.hero = n % 5 == 0;
			if ( o.hero ){	// 英雄坦克比较厉害，所以速度，攻防都较高
				o.speed = 4; o.gong = 2; o.fang = 2;
				o.filename = "d";
				o.img.src = "img/" + o.filename + o.dir + ".gif";
			}
			o.kongzhitimer = setInterval(function(){
				if ( zhuangtai == zt[0] || zhuangtai == zt[2] || zhuangtai == zt[5] )
					return;
				n = parseInt(Math.random()*100);
				if ( n % 39 == 0 ){		// 决定是否可以改变方向
					n = parseInt(Math.random()*4);
					if ( 0 == n ){
						o.toUp();
					} else if ( n == 1 ){
						o.toDown();
					} else if ( n == 2 ){
						o.toLeft();
					} else if ( n == 3 ){
						o.toRight();
					}
				}
				var k = parseInt(Math.random()*100);
				if ( k % 29 == 0 ){
					o.fire();
				}
			}, 30);
			return o;
		}
		
		function initWall(){
			var x = (can.width - wallcount * WALL_W) / 2;
			var y = 150;
			for ( var i = 0; i < wallcount; i ++ ){
				var o = new Wall(x, y, 2);
				stage.add(o);	x += WALL_W;
			}
			 x = can.width-200;
		     y = (can.height-WALL_H)/2-130;
			for ( var i = 0; i < wallcount-20; i ++ ){
				var o = new Wall(x, y, 2);
				stage.add(o);	y += WALL_W;
			}
			x = can.width-1200;
		    y = (can.height-WALL_H)/2-130;
			for ( var i = 0; i < wallcount-20; i ++ ){
				var o = new Wall(x, y, 2);
				stage.add(o);	y += WALL_W;  x-=WALL_W;
			}
			x = can.width -1200;
		    y = (can.height-WALL_H)/2+20;
			for ( var i = 0; i < wallcount-24; i ++ ){
				var o = new Wall(x, y, 2);
				stage.add(o);	x += WALL_W;
			}
			x = can.width -600;
		    y = (can.height-WALL_H)/2+20;
			for ( var i = 0; i < wallcount-24; i ++ ){
				var o = new Wall(x, y, 2);
				stage.add(o);	x += WALL_W;
			}
			x = can.width -600;
		    y = (can.height-WALL_H)/2+55;
			for ( var i = 0; i < wallcount-26; i ++ ){
				var o = new Wall(x, y, 2);
				stage.add(o);	x += WALL_W;
			}
			x = can.width -600;
		    y = (can.height-WALL_H)/2+90;
			for ( var i = 0; i < wallcount-26; i ++ ){
				var o = new Wall(x, y, 2);
				stage.add(o);	x += WALL_W;
			}
			x = can.width -510;
		    y = (can.height-WALL_H)/2+125;
			for ( var i = 0; i < wallcount-26; i ++ ){
				var o = new Wall(x, y, 2);
				stage.add(o);	y += WALL_W;
			}
			x = (can.width - wallcount * WALL_W) / 2;
			y = (can.height-WALL_H)/2+290;
			for ( var i = 0; i < wallcount-2; i ++ ){
				var o = new Wall(x, y, 2);
				stage.add(o);	x += WALL_W;
			}
			x = can.width -455;
		    y = (can.height-WALL_H)/2+325;
			for ( var i = 0; i < wallcount-26; i ++ ){
				var o = new Wall(x, y, 2);
				stage.add(o);	y += WALL_W;
			}
			x = can.width -400;
		    y = (can.height-WALL_H)/2+195;
			for ( var i = 0; i < wallcount-17; i ++ ){
				var o = new Wall(x, y, 2);
				stage.add(o);	x += WALL_W;
			}
			
			
		}
		
		function initEnemy(){
			var x = (can.width - enemycount * DI_W - enemycount * 5) / 2-60;
			var y = 80;
			for ( var i = 0; i < enemycount; i ++ ){
				var o = new Enemy(x, y);
				stage.add(o);	x += DI_W + 10;
			}
			x = (can.width - enemycount * DI_W - enemycount * 5) / 2;
		    y = (can.width - enemycount * DI_H - enemycount * 5) / 2-70;
		    var e1 = 3;  enemycount+= e1;
			for ( var i = 0; i < e1; i ++ ){
				var o = new Enemy(x, y);
				stage.add(o);	x += DI_W + 20;
			}
			x = (can.width - enemycount * DI_W - enemycount * 5) / 2+650;
		    y = (can.width - enemycount * DI_H - enemycount * 5) / 2-10;
		    var e2 = 5;  enemycount+= e2;
			for ( var i = 0; i < e2; i ++ ){
				var o = new Enemy(x, y);
				stage.add(o);	x += DI_W +5;
			}
			x = can.width -400;
		    y =(can.width - enemycount * DI_H - enemycount * 5) / 2+280;
		    var e3 = 4;  enemycount+= e3;
			for ( var i = 0; i < e3; i ++ ){
				var o = new Enemy(x, y);
				stage.add(o);	x += DI_W + 10;
			}
			
		}
		
		function initHome(){
			var x = mm.x - 35, y = mm.y - 35;
			var o = new Wall(x, y, 3);	o.ishome = true;
			stage.add(o);	x += WALL_W;		zhb.push(o);
			
			o = new Wall(x, y, 3);		o.ishome = true;
			stage.add(o);	x += WALL_W;		zhb.push(o);
			
			o = new Wall(x, y, 3);		o.ishome = true;
			stage.add(o);	x += WALL_W;		zhb.push(o);
			
			o = new Wall(x, y, 3);		o.ishome = true;
			stage.add(o);	x += WALL_W;		zhb.push(o);
			
			x = mm.x - 35, y = y + WALL_H;
			
			o = new Wall(x, y, 3);		o.ishome = true;
			stage.add(o);	x += WALL_W*3;		zhb.push(o);
			o = new Wall(x, y, 3);		o.ishome = true;
			stage.add(o);	x += WALL_W*3;		zhb.push(o);
			
			x = mm.x - 35, y = y + WALL_H;
			
			o = new Wall(x, y, 3);	o.h += 15;		o.ishome = true;
			stage.add(o);	x += WALL_W*3;		zhb.push(o);
			o = new Wall(x, y, 3);	o.h += 15;		o.ishome = true;
			stage.add(o);	x += WALL_W*3;		zhb.push(o);
		}
		
		function anxiaqu(){
			var c = event.keyCode;
			if ( c == 89 && zhuangtai == zt[0] ) {
				zhuangtai = zt[1];
				clearInterval(kaishitimer);
				bofang("kaipao.wav");
				gametimer = setInterval(function(){
					if ( zhuangtai == zt[2] ) return;
					gametime --;
					
					var n = parseInt(Math.random() * 100);
					if ( n % 3 == 0 ){
						if ( fjcount < 10 ){
							var fj = new Plane();
							stage.add(fj);
							fjcount ++;
						}
					}
					
					if ( gametime == 0 ){
						mm.die();
						clearInterval(gametimer);
					}
				}, 1000);
			} else if ( c == 80 && zhuangtai != zt[0] ){
				if ( zhuangtai == zt[3] || zhuangtai == zt[4] || zhuangtai == zt[5] 
					|| zhuangtai == zt[6] || zhuangtai == zt[8]) return;
				zhuangtai = zhuangtai == zt[2] ? zt[1] : zt[2];		// 暂停和恢复游戏
				if ( zhuangtai == zt[2] ){
					wo.left = false; wo.right = false; wo.up = false; wo.down = false;
				}
			}
			if ( zhuangtai == zt[0] || zhuangtai == zt[2] || zhuangtai == zt[4]  
						|| zhuangtai == zt[6] || zhuangtai == zt[8] || zhuangtai == zt[3] ){
				return;
			}
			if ( 65 == c||37==c ){
				wo.toLeft();
			} else if (87 == c||38==c ){
				wo.toUp();
			} else if ( 68 == c||39==c ){
				wo.toRight();
			} else if ( 83 == c||40==c ){
				wo.toDown();
			}
		}
		
		function songkai(){
			var c = event.keyCode;
			if ( c == 82 && zhuangtai == zt[8] ){
				wo.relife();
			}
			if ( zhuangtai == zt[0] || zhuangtai == zt[2] || zhuangtai == zt[4]  
						|| zhuangtai == zt[6] || zhuangtai == zt[8] ){
				return;
			}
			if ( 65 == c||37==c ){
				wo.stopLeft();
			} else if ( 87 == c||38==c ){
				wo.stopUp();
			} else if ( 68 == c||39==c ){
				wo.stopRight();
			} else if ( 83 == c||40==c ){
				wo.stopDown();
			} else if ( 32 == c||13== c ){
				if ( wo.life <= 0 ) return;
				wo.fire();
				bofang("attack.mp3");
			} else if ( c == 66 ){
				wo.mailei();
			} else if ( c == 65 ){
				wo.dajueji();
			}
		}
		
		/**检查o对象是否超出屏幕范围**/
		function checkOut(o){
			return o.x < 0 || o.y < 0 || o.x > can.width - o.w || o.y > can.height - o.h;
		}
		
		
		/**当前物体o与界面上所有物体的碰撞检验**/
		function pengzhuang(o){
			var r = new Object();
			r.peng = false;
			var list = stage.data;
			for ( var i in list ){
				var m = list[i];
				if ( m.index == o.index ) continue;					// 避开自己与自己的碰撞
				if ( m.type == TUPIAN ) continue;					// 避开子弹与图片的碰撞
				if ( m.type == DAODAN ) continue;					// 避开与炸弹的碰撞
				if ( o.type == WOJUN && m.type == Bullet ) continue;	// 避开坦克与子弹的碰撞
				if ( o.type == Enemy && m.type == Bullet ) continue;	// 避开坦克与子弹的碰撞
				if ( o.type == Bullet && m.type == Bullet ) continue;	// 避开子弹与子弹的碰撞
				if ( o.type == Bullet && m.type == DAOJU ) continue;	// 避开子弹与道具的碰撞
				if ( o.type == WOJUN && m.type == mine ) continue;	// 避开我军与地雷的碰撞
				if ( o.type == Bullet && m.type == mine ) continue;	// 避开子弹与地雷的碰撞
				
				if ( o.type == Enemy && m.type == Plane ) continue;	// 避开敌军与飞机的碰撞
				if ( o.type == WOJUN && m.type == Plane ) continue;	// 避开我军与飞机的碰撞
				if ( o.type == Plane ) 					 continue;	// 避开飞机与墙的碰撞
				if ( o.type == Bullet && o.tank.type == Enemy && m.type == Plane ) continue;	// 避开敌人子弹与飞机的碰撞
				if ( o.type == Bullet && o.tank.type == Enemy && m.type == Enemy) continue;	// 避开子弹与盟军的碰撞
				if ( hitForRectangle(o, m) ) {
					r.peng = true;
					r.o = m;
					return r;
				}
			}
			return r;
		}
		
		/**炸弹伤害碰撞**/
		function zhadanpengzhuang(o){
			var r = new Object();
			r.peng = false;
			var list = stage.data;
			for ( var i in list ){
				var m = list[i];
				if ( m.index == o.index ) continue;					// 避开自己与自己的碰撞
				if ( m.type == TUPIAN ) continue;					// 避开子弹与图片的碰撞
				if ( m.type == DAODAN ) continue;					// 避开与炸弹的碰撞
				if ( o.type == WOJUN && m.type == Bullet ) continue;	// 避开坦克与子弹的碰撞
				if ( o.type == Enemy && m.type == Bullet ) continue;	// 避开坦克与子弹的碰撞
				if ( o.type == Bullet && m.type == Bullet ) continue;	// 避开子弹与子弹的碰撞
				if ( o.type == Bullet && m.type == DAOJU ) continue;	// 避开子弹与道具的碰撞
				if ( o.type == WOJUN && m.type == mine ) continue;	// 避开我军与地雷的碰撞
				if ( o.type == Bullet && m.type == mine ) continue;	// 避开子弹与地雷的碰撞
				
				if ( o.type == Enemy && m.type == Plane ) continue;	// 避开敌军与飞机的碰撞
				if ( o.type == WOJUN && m.type == Plane ) continue;	// 避开我军与飞机的碰撞
				if ( o.type == Plane ) 					 continue;	// 避开飞机与墙的碰撞
				if ( o.type == Bullet && o.tank.type == Enemy && m.type == Plane ) continue;	// 避开敌人子弹与飞机的碰撞
				if ( o.type == Bullet && o.tank.type == Enemy && m.type == Enemy) continue;	// 避开子弹与盟军的碰撞
				if ( hitForRectangle(o, m) ) {
					r.peng = true;
					r.o = m;
					if ( r.o.type == WOJUN ){
						r.o.shixue(o.gong);
					} else if ( r.o.type == Enemy || r.o.type == QIANG || r.o.type == CMD ) {
						r.o.shixue(r.o.fang + 15);	// 被炸的失血5
					}
				}
			}
			return r;
		}
		
		/**
		 * 矩形物体碰撞检验
		 * @param {Object} o1
		 * @param {Object} o2
		 */
		function hitForRectangle(o1,o2){
			if ( o1.x < o2.x )
			{
				if ( o2.x - o1.x <= o1.w ){
					if ( o1.y < o2.y ){
						return o2.y - o1.y <= o1.h;
					} else if ( o1.y > o2.y ){
						return o1.y - o2.y <= o2.h;
					} else 
						return o1.y == o2.y;
				}
			} else if ( o1.x > o2.x )
			{
				if ( o1.x - o2.x <= o2.w ){
					if ( o1.y < o2.y ){
						return o2.y - o1.y <= o1.h;
					} else if ( o1.y > o2.y ){
						return o1.y - o2.y <= o2.h;
					} else 
						return o1.y == o2.y;
				}
			} else {
				if ( o1.y < o2.y ){
					return o2.y - o1.y <= o1.h;
				} else if ( o1.y > o2.y ){
					return o1.y - o2.y <= o2.h;
				} else 
					return o1.y == o2.y;
			}
		 	return false;
		}
		
		function bofang(src){
			var audio = new Audio();
			audio.src = "sounds/" + src;
			audio.play();
		}
		
		function jiandaoju(o, dj){
			dj.die();
			if ( dj.n == 0 ){						// 提速
				o.speed ++;
				o.speed = o.speed > 7 ? 7 : o.speed;
				if ( o.type == WOJUN )	bofang("speed.wav");
			} else if ( dj.n == 1 ){				// 奖励生命和多出敌人
				if ( o.type == WOJUN ){
					o.fuhuo ++;
					o.fuhuo = o.fuhuo > 6 ? 6 : o.fuhuo;
					createNewEnemy(2);
				} else {
					createNewEnemy(5);
				}
				if ( o.type == WOJUN )	bofang("prop.mp3");
			} else if ( dj.n == 2 ){				// 冰冻对方
				if ( zhuangtai == zt[7] ) return;
				if ( o.type == WOJUN ){
					changeState(zt[5]);
					bofang("daxiao.wav");
				} else {
					changeState(zt[6]);
					bofang("ice.wav");
				}
			} else if ( dj.n == 3 ){				// 火力升级
				o.gong ++;
				o.gong = o.gong > 4 ? 4 : o.gong;
				if ( o.type == WOJUN )	bofang("prop.wav");
			} else if ( dj.n == 4 ){				// 防御升级
				o.fang ++;
				o.fang = o.fang > 4 ? 4 : o.fang;
				if ( o.type == WOJUN )	bofang("yongbao.wav");
			} else if ( dj.n == 5 ){				// 加固指挥部
				if ( o.type == WOJUN ){
					Headquarters(20);
					bofang("yongbao.wav");
				} else {
					Headquarters(-20);
				}
			} else if ( dj.n == 6 ){				// 加血
				o.life += 30;
				o.life = o.life > 100 ? 100 : o.life;
				if ( o.type == WOJUN )	bofang("prop.wav");
			} else if ( dj.n == 7 ){				// 失血
				if ( zhuangtai == zt[7] && o.type == WOJUN ) return;
				o.life -= (parseInt(Math.random() * 30) + 60);
				if ( o.life <= 0 ) {
					o.die();
				}
				bofang("bomb1.wav");
			} else if ( dj.n == 8 ){				// 给指挥官加血
				if ( o.type == WOJUN ){
					mm.life += 30;
					mm.life = mm.life > 100 ? 100 : mm.life;
					bofang("yongbao.mp3");
				} else {
					add_blood();
					createNewEnemy(4);
				}
			} else if ( dj.n == 9 ){				// 加地雷2颗
				if ( o.type == WOJUN ){
					o.mine += 2;
					o.mine = o.mine > 10 ? 10 : o.mine;
					bofang("prop.wav");
				} else {
					shengfang();
					createNewEnemy(3);
				}
			}
		}
		
		function add_blood(){
			var list = stage.data;
			for ( var i = 0; i < list.length; i ++ ){
				if ( list[i].type == Enemy ){		// 给所有敌军加血
					list[i].life += 10 + parseInt(Math.random()*20);
					list[i].life = list[i].life > 100 ? 100 : list[i].life;
				}
			}
		}
		
		function shengfang(){
			var list = stage.data;
			for ( var i = 0; i < list.length; i ++ ){
				if ( list[i].type == Enemy ){		// 给所有敌军升防
					list[i].fang ++;
					list[i].fang = list[i].fang > 4 ? 4 : list[i].fang;
				}
			}
		}
		
		function createNewEnemy(count){
			if ( enemycount > 27 ) return;
			for ( var i = 0; i < count; i ++ ){
				var x = parseInt(Math.random() * (can.width -200)) + 80;
				var y = parseInt(Math.random() * (can.height-150)) + 50;
				var o = new Enemy(x, y);
				var r = pengzhuang(o);
				while ( r.peng ){
					x = parseInt(Math.random() * (can.width -200)) + 80;
					y = parseInt(Math.random() * (can.height-150)) + 50;
					o.x = x; o.y = y;
					r = pengzhuang(o);
				}
				stage.add(o);
			}
			enemycount += count;
			gametime += 50;
			bofang("newenemy.wav");
		}
		
		function Headquarters(xue){
			for ( var i in zhb ){
				if ( xue > 0 && zhb[i].life <= 0 ){
					stage.add(zhb[i]);
				}
				zhb[i].life += xue;
				zhb[i].life = zhb[i].life > 100 ? 100 : zhb[i].life;
				if ( zhb[i].life <= 0 ){
					zhb[i].die();
				}
			}
		}
		
		function changeState(st){
			if ( zhuangtai == zt[3] || zhuangtai == zt[4] || zhuangtai == zt[8]
					|| zhuangtai == zt[5] || zhuangtai == zt[6] ) return;
			tempstate = zhuangtai;			// 记录一下冰冻以前的状态
			zhuangtai = st;
			// 启动解冻定时器
			bdtimer = setInterval(function(){
				bdshijian --;
				if ( bdshijian <= 0 ){
					bdshijian = 5;
					clearInterval(bdtimer);
					zhuangtai = zhuangtai == zt[5] || zhuangtai == zt[6] ? tempstate : zhuangtai; 
				}
			}, 1000);
		}
		
		function over(){
			if ( overtimer == null ) {
				overtimer = setInterval(function(){
					overy -= 10;
					if ( overy <= can.height / 2) {
						clearInterval(overtimer);
					}
				}, 50);
			}
			bofang("lost.wav");
		}
		
		function move(o){
			if ( zhuangtai == zt[2] ) return ;
			if ( o.type == Enemy ){
				if ( zhuangtai == zt[0] || zhuangtai == zt[5] ) {
					return;
				}
			} else {
				if ( zhuangtai == zt[6] ) {
					o.left = false; o.right = false; o.up = false; o.down = false;
					return;
				}
			}
			var ox = o.x, oy = o.y;	// 保存移动前的旧坐标
			if ( o.left ){
				o.x -= o.speed;
			} else if ( o.right ){
				o.x += o.speed;
			} else if ( o.up ){
				o.y -= o.speed;
			} else if ( o.down ){
				o.y += o.speed;
			}
			if ( checkOut(o) ){		// 移动后，发现坐标超出范围，就恢复为旧坐标
				o.x = ox;	o.y = oy;
			}
			var r = pengzhuang(o);
			if ( r.peng ){			// 是否发其他物体发生了碰撞，如果有就恢复为旧坐标
				if ( r.o.type == DAOJU ){
					o.eat(r.o);		// 吃道具
				} if ( r.o.type == mine ) {
					r.o.die();
					var bz = new Blast(r.o);
					stage.add(bz);
					var m = 40 + parseInt(Math.random() * 55);
					o.shixue(m);
					bofang("hedan.wav");
				} else {
					o.x = ox;	o.y = oy;
				}
			}
		}
		
		function check_up(o){
			var r = pengzhuang(o);
			while ( r.peng && (r.o.type == Enemy || r.o.type == QIANG ) ) {
				if ( o.x > can.width/2 ) {
					o.x -= 10;
				} else {
					o.x += 10;
				}
				if ( o.y > can.height/2 ){
					o.y -= 10;
				} else {
					o.y += 10;
				}
				r = pengzhuang(o);
			}
		}
		
		// 定义一个存储器对象
		var stage = new Stage();
		
		var wo = new Allies(300, 600);
		wo.gong = 1;	wo.speed = 7;
		var mm = new Command((can.width-MM_W)/2, (can.height-MM_H), 2);
		
		stage.add(mm);
		
		// 构造一排障碍墙
		initWall();
		
		// 构造一排敌军
		initEnemy();
		
		// 构造指挥部
		initHome();
		
		stage.add(wo);
		stage.render();
		
		kaishitimer = setInterval(function(){ kaishicolor = ! kaishicolor }, 400);

		addEventListener("keydown", anxiaqu);
		addEventListener("keyup", songkai);
		setTimeout(function(){ bofang("start.mp3");}, 200);
		</script>
	</body>
</html>